import { fetchMenuTreeByTenantId, fetchMenuTreeByRoleId, doRemoveList, getRolesByCreateUserPage, addRole, editRole } from '@/services/user';
export default {
  namespace: 'authManage',
  state: {
    total: 0,
    items: [],
    loading: true
  },
  effects: {
    *getRoleLists({ payload, callback }, { call, put }) {
      const response = yield call(getRolesByCreateUserPage, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *removeList({ payload, callback }, { call, put }) {
      const response = yield call(doRemoveList, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getMenuTreeByTenantId({ payload, callback }, { call, put }) {
      const response = yield call(fetchMenuTreeByTenantId, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getMenuTreeByRoleId({ payload, callback }, { call, put }) {
      const response = yield call(fetchMenuTreeByRoleId, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addAtuhRole({ payload, callback }, { call, put }) {
      const response = yield call(addRole, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *editAtuhRole({ payload, callback }, { call, put }) {
      const response = yield call(editRole, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
